import Mock from 'mockjs'

const username = ['admin', 'user']
const userInfo = {
  id: Mock.mock('@guid'),
  username: Mock.mock('@name'),
  email: Mock.mock('@email'),
  avatar: 'https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png',
  roles: [1],
  state: 1,
  phone: 19951123456
}

const userMenu = [
  {
    id: 2,
    name: 'components',
    code: 'components',
    status: 1,
    type: 0,
    children: [
      {
        id: 21,
        name: 'richTextEditor',
        code: 'components:richTextEditor',
        status: 1,
        type: 1
      },
      {
        id: 22,
        name: 'markdown',
        code: 'components:markdown',
        status: 1,
        type: 1
      }
    ]
  },
  {
    id: 11,
    name: 'about',
    code: 'about',
    status: 1,
    type: 0
  }
]

const LoginAPI =  {
  login: (config: { body: string }) => {
    const body = JSON.parse(config.body)
    if (username.includes(body.username)) {
      return {
        code: 1,
        data: { token: Mock.mock('@guid') },
        msg: 'ok'
      }
    } else {
      return {
        code: 0,
        msg: '用户名或密码错误'
      }
    }
  },
  userInfo: () => {
    return {
      code: 1,
      data: userInfo,
      msg: 'ok'
    }
  },
  userMenu: () => {
    return {
      code: 1,
      data: userMenu,
      msg: 'ok'
    }
  },
  logout: () => {
    return {
      code: 1,
      data: 'success',
      msg: 'ok'
    }
  }
}

// Mock 登录等接口
Mock.mock(/\/api\/auth\/login/, 'post', LoginAPI.login)
Mock.mock(/\/api\/auth\/logout/, 'post', LoginAPI.logout)
Mock.mock(/\/api\/auth\/userinfo/, 'post', LoginAPI.userInfo)
Mock.mock(/\/api\/auth\/menutree/, 'post', LoginAPI.userMenu)